// 文件上传

// 图片上传
const iupload_image = async ctx => {
    let files = ctx.req.files, flag = false;

    files.map(item => {
        if (item.mimetype == "image/jpeg" || item.mimetype == "image/jpg") {
            item.path = ctx.origin + item.path.replace('public', '').replace(/\\/g, '/')
            flag = true
        } else {
            flag = false
        }
    })

    if (flag) {
        ctx.body = {
            status: true,
            msg: '图片上传成功',
            results: files
        }
    } else {
        ctx.body = {
            status: false,
            msg: '图片上传失败，格式为jpeg,jg'
        }
    }
}

// 修改头像
const update_avatar = async ctx => {
    let file = ctx.req.file

    if (file.mimetype == "image/jpeg" || file.mimetype == "image/jpg") {
        file.path = ctx.origin + file.path.replace('public', '').replace(/\\/g, '/')
        ctx.body = {
            status: true,
            msg: '图片上传成功',
            results: file
        }
    } else {
        ctx.body = {
            status: false,
            msg: '图片上传失败，格式为jpeg,jg'
        }
    }
}

// 富文本编译器图片上传
const editor = async ctx => {
    let file = ctx.req.file

    if (file.mimetype == "image/jpeg" || file.mimetype == "image/jpg") {
        file.path = ctx.origin + file.path.replace('public', '').replace(/\\/g, '/')
        ctx.body = {
            errno: 0,
            data: [{
                url: file.path,
                alt: '',
                href: ''
            }]
        }
    } else {
        ctx.body = {
            status: false,
            msg: '图片上传失败，格式为jpeg,jg'
        }
    }
}

module.exports = {
    iupload_image,
    update_avatar,
    editor
}
